<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;

/**
 * 文件控制器     用于文件上传和下载
 * @author derek <lixm@my818.com>
 */
class AttachmentController extends AdminController {

    /**
     * 文件上传 
     * @author derek <lixm@my818.com>
     */
    public function upload(){
		$return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
		/* 调用文件上传组件上传文件 */
		$attLogic = D('Attachment', 'Logic');
		$file_driver = C('UPLOAD_DRIVER');
		
		$info = $attLogic->upload(
			$_FILES,
			C('UPLOAD_FILE'),
			$file_driver,
			C("UPLOAD_{$file_driver}_CONFIG")
		);

        /* 记录附件信息 */
        if($info){
            $return['data'] = $info['download']['att_id'];
            $return['info'] = $info['download']['name'];
            $return['fullpath'] = $info['download']['fullpath'];
        } else {
            $return['status'] = 0;
            $return['info']   = $attLogic->getError();
        }

        /* 返回JSON数据 */
        $this->ajaxReturn($return);
    }

    /**
     * 上传头像
     * @author derek <lixm@my818.com>
     */
    public function uploadPhoto(){

        /* 返回标准数据 */
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');

        /* 调用文件上传组件上传文件 */
        $attLogic = D('Attachment', 'Logic');
        
        $pic_driver = C('UPLOAD_DRIVER');
        
        $info = $attLogic->upload(
            $_FILES,
            C('UPLOAD_PICTURE_CONFIG'),
            $pic_driver,
            C("UPLOAD_{$pic_driver}_CONFIG")
        ); 

        /* 记录图片信息 */
        if($info){
            $return['att_id'] = $info['download']['att_id'];
            $return['info'] = $info['download']['name'];
            $return['fullpath'] = $info['download']['fullpath'];
        } else {
            $return['status'] = 0;
            $return['info']   = $attLogic->getError();
        }

        /* 返回JSON数据 */
        $this->ajaxReturn($return);
    }
    
    /**
     * 下载文件
     * @author derek <lixm@my818.com>
     */
    public function download(){
        $attID = I('att_id');
        
        if(empty($attID) || !is_numeric($attID)){
            $this->error('参数错误！');
        }
        
        $attLogic = D('Attachment', 'Logic');
        
        if(!$attLogic->download($attID)){
            
            $this->error($attLogic->getError());
        }
    }
}
